﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        private static DataTable getTable = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }
        private void FillData()
        {
            var sqlString = "select *  from Articles";
            getTable = DbHelper.getData(sqlString);
            ggb.DataSource = getTable;
            ggb.DataBind();
        }

        private string GetCellValue(int rowIndex, int colIndex)
        {
            var control = ggb.Rows[rowIndex].Cells[colIndex];
            string re = "";
            if (control.Controls.Count > 0)
            {
                re = ((TextBox)control.Controls[0]).Text;
            }
            else
            {
                re = control.Text;
            }
            return re;
        }

        protected void ggb_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetCellValue(e.RowIndex, 0);
            var sql = string.Format("delete from Articles where Id={0}", id);
            DbHelper.CMA(sql);
            ggb.EditIndex = -1;
            FillData();
        }

        protected void ggb_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ggb.EditIndex = -1;
            FillData();
        }

        protected void ggb_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetCellValue(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);

            var Title = GetCellValue(e.RowIndex, 1);
            var Content = GetCellValue(e.RowIndex, 2);
            var Author = GetCellValue(e.RowIndex, 3);
            var Remarks = GetCellValue(e.RowIndex, 4);
            if (isOk)
            {
                var sql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id='{4}'", Title, Content, Author, Remarks, id);
                DbHelper.CMA(sql);
            }
            else
            {
                var sql = string.Format("insert into Articles (Title,Content,Author,Remarks) values('{0}','{1}','{2}','{3}')", Title, Content, Author, Remarks);
                DbHelper.CMA(sql);
            }
            ggb.EditIndex = -1;
            FillData();
        }

        protected void ggb_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ggb.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void add_Click(object sender, EventArgs e)
        {
            var dt = getTable.Copy();
            dt.Rows.Add(dt.NewRow());

            getTable = dt;

            ggb.DataSource = getTable;
            ggb.DataBind();

            ggb.EditIndex = dt.Rows.Count - 1;
            ggb.DataSource = dt;
            ggb.DataBind();
        }
    }
}